//
//  MJHeader.m
//  团购
//
//  Created by app43 on 15/6/21.
//  Copyright (c) 2015年 www.xcu.edu.cn. All rights reserved.
//

#import "MJHeader.h"

const int MJImageCount = 5;

@interface MJHeader ()

<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property (strong, nonatomic) NSTimer *timer;

@end


@implementation MJHeader

+ (instancetype)header
{
    return [[NSBundle mainBundle] loadNibNamed:@"MJHeader" owner:nil options:nil][0];
}

- (void)awakeFromNib
{
    // 1.添加图片
    CGFloat imageW = self.scrollView.frame.size.width;//kuan
    
    CGFloat imageH = self.scrollView.frame.size.height;//gao
    
    CGFloat imageY = 0;//y
    
    for (int index = 0; index < MJImageCount; index++) {//x =5*kuan
        UIImageView *imageView = [[UIImageView alloc] init];
        
        CGFloat imageX = index * imageW;
        
        imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);//ImageView 的大小位置
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ad_%02d", index]];// 图片
        
        [self.scrollView addSubview:imageView];//把图片加载到SCrollView
    }
    
    
    //属性
    
    
    
    // 2.设置内容尺寸
    self.scrollView.delegate = self;
    
    self.scrollView.contentSize = CGSizeMake(imageW * MJImageCount, 0);//可以滑动
    
    self.scrollView.pagingEnabled = YES;//反弹
    
    // 3.分页
    self.pageControl.numberOfPages = MJImageCount;//分页pageControl
    
    // 4.定时器
    self.timer = [NSTimer timerWithTimeInterval:2.5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)nextImage
{
    // 1.下一页                                 5-1
    if (self.pageControl.currentPage == MJImageCount - 1) {
        self.pageControl.currentPage = 0;
    } else {
        self.pageControl.currentPage++;
    }
    
    // 2.设置滚动        NSpoint    宽和高                       x=一个图片的宽*当前的currentPage， y=0
    CGPoint offset = CGPointMake(self.scrollView.frame.size.width * self.pageControl.currentPage, 0);
    
    [self.scrollView setContentOffset:offset animated:YES];//设置偏移量
}


//当用手滑动停止时调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //据需调用Nstimer
    self.timer = [NSTimer timerWithTimeInterval:2.5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}


//当手刚开始拽时调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
    //定时器失效
    [self.timer invalidate];
    self.timer = nil;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    //scrollView在滑动中调用
    //设置page。当前page。
    //if (self.timer) return;
    self.pageControl.currentPage = (scrollView.contentOffset.x + scrollView.frame.size.width * 0.5) / scrollView.frame.size.width;
}

@end
